package org.munoz.client;

import java.util.ArrayList;

import org.munoz.shared.Card;
import org.munoz.shared.Deck;
import org.munoz.shared.Player;
import org.munoz.shared.Rank;
import org.munoz.shared.State;

/**
 * @author jpm
 * This class creates an AI that is used to play a single player
 */
public class AIPlayer {
	private Player robot;
	private ArrayList<Rank> cardsRequestedAndGivenMemory;
	
	/**
	 * Constructs an AIPlayer object
	 */
	AIPlayer(){
		robot = new Player(State.PLAYER2);
		cardsRequestedAndGivenMemory = new ArrayList<Rank>();
	}

	/**
	 * Sets the player used by this AI
	 * @param player
	 */
	public void setPlayer(Player player) {
		this.robot = player;
	}
	
	/**
	 * @return the player associated with this AI
	 */
	public Player getPlayer(){
		return robot;
	}

	/**
	 * @return a rank to request depending on its previous actions. 
	 */
	public Rank requestCardTurn(){
		// Clean memory by observing the table
		for(Rank r : robot.getPlayerFourRankCardsPlacedInTable()){
			cardsRequestedAndGivenMemory.remove(r);
		}
		// First check memory & hand & cards 
		Rank rank = null;
		for(Rank r : cardsRequestedAndGivenMemory){
			for(Card c : robot.getHand().getCards()){
				if(r == c.getRank()){
					rank = r;
				}
			}
		}
		if(rank == null && !robot.getHand().isEmpty()){
			return robot.getHand().getDeck(0).getRank();
		}
		return rank;
	}

	/**
	 * @param adds a car that was requested to this AI and given to the other player
	 */
	public void addMemory(Rank requested) {
		cardsRequestedAndGivenMemory.add(requested);
	}
	
}
